package barbershop.com.controller;

import barbershop.com.annotation.LoginToken;
import barbershop.com.entity.Cashier;
import barbershop.com.service.CashierService;
import barbershop.com.service.TokenService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cashier")
public class CashierController {
    @Autowired
    private CashierService cashierService;
    @Autowired
    private TokenService tokenService;

   @RequestMapping(value = "/login", method = RequestMethod.POST)
   public Object login(@RequestParam(required = false)String casName,
                       @RequestParam(required = false)String casPwd){
       JSONObject jsonObject=new JSONObject();
       Cashier cashier=cashierService.login(casName, casPwd);
       if(cashier==null){
           jsonObject.put("message","登录失败,用户不存在");
           return jsonObject;
       }else {
           if (!cashier.getCasPwd().equals(casPwd)){
               jsonObject.put("message","登录失败,密码错误");
               return jsonObject;
           }else {
               String token = tokenService.getCasToken(cashier);
               jsonObject.put("token", token);
               jsonObject.put("cashier", cashier);
               return jsonObject;
           }
       }
   }

    @RequestMapping("/add")
    public Cashier addRole(@RequestBody Cashier record){
        return this.cashierService.insert(record);
    }

    @RequestMapping("/delete")
    public Boolean deleteRole(@RequestBody Cashier record){
        return this.cashierService.delete(record);
    }

    @RequestMapping("/update")
    public Cashier updateRole(@RequestBody Cashier record){
        return this.cashierService.update(record);
    }

    @RequestMapping("/find")
    public Cashier findRole(@RequestBody Cashier record){
        return this.cashierService.find(record);
    }

    @LoginToken
    @GetMapping("/getMessage")
    public String getMessage(){
        return "你已通过验证";
    }
}
